﻿var BonusTypes = new Array();

// przeznaczenie bonusów
var BonusDestiny = {
    ME: 0, // dla mnie
    THEY: 1, // dla przeciwnikow
    ALL: 2 // dla wszystkich
}

// nazwy bonusów
var BonusNames = {
    SPEED_ME: {value: 0, text: "SPEED_ME"},
    SPEED_THEY: { value: 1, text: "SPEED_THEY" },
    SLOW_ME : {value: 2, text: "SLOW_ME"},
    SLOW_THEY: { value: 3, text: "SLOW_THEY" },
    FAT_ME: { value: 4, text: "FAT_ME" },
    FAT_THEY: { value: 5, text: "FAT_THEY" },
    THIN_ME: { value: 6, text: "THIN_ME" },
    THIN_THEY: { value: 7, text: "THIN_THEY" },
    FLYING: { value: 8, text: "FLYING" },
    CLEAN_ALL: { value: 9, text: "CLEAN_ALL" },
    CLEAN_ME: { value: 10, text: "CLEAN_ME" },
    WALL_ALL: { value: 11, text: "WALL_ALL" }
}

//klasa reprezentujaca typ bonusu
function BonusType(destiny, name, time, image, changeSnakeParameter) {
    this.destiny = destiny; // przeznaczenie
    this.name = name; // nazwa
    this.time = time; // czas jaki bonus bedzie wyswietlany
    this.image = image; // obrazek
    this.changeSnakeParameter = changeSnakeParameter;
}

// inicjalizacja bonusów
function InitializeBonuses() {
    bonusCanvas = (function () {
        var canvas = document.createElement("canvas");
        canvas.width = gameWidth;
        canvas.height = gameHeight;
        return canvas;
    })();
    ctxBonus = bonusCanvas.getContext("2d");
    BonusTypes.push(new BonusType(BonusDestiny.ME, BonusNames.SPEED_ME, 5000, loadImg('Images/speed_me.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.THEY, BonusNames.SPEED_THEY, 3000, loadImg('Images/speed_they.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.ME, BonusNames.FAT_ME, 5000, loadImg('Images/fat_me.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.THEY, BonusNames.FAT_THEY, 3000, loadImg('Images/fat_they.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.ME, BonusNames.THIN_ME, 5000, loadImg('Images/thin_me.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.THEY, BonusNames.THIN_THEY, 5000, loadImg('Images/thin_they.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.ME, BonusNames.SLOW_ME, 5000, loadImg('Images/slow_me.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.THEY, BonusNames.SLOW_THEY, 5000, loadImg('Images/slow_they.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.ME, BonusNames.FLYING, 5000, loadImg('Images/flying.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.ME, BonusNames.CLEAN_ME, 0, loadImg('Images/clean_me.png'), true));
    BonusTypes.push(new BonusType(BonusDestiny.ALL, BonusNames.CLEAN_ALL, 0, loadImg('Images/clean_all.png'), false));
    BonusTypes.push(new BonusType(BonusDestiny.ALL, BonusNames.WALL_ALL, 7000, loadImg('Images/wall_all.png'), true));
}

//zaladowanie obrazka
function loadImg(path) {
    var image = new Image();
    image.src = path;
    return image;
}

function ChangeSnakeParameter(snake, bonusType) {
    snake.bonusFinishing = true;
    // SPEED
    if (bonusType.name == BonusNames.SPEED_ME || bonusType.name == BonusNames.SPEED_THEY) {
        console.log(snake.speed);
        snake.speed += 0.05;
        //snake.radius += 10;
        console.log('changed SPEED');
        console.log(snake.speed);
        setTimeout(function () {
            if (snake.bonusFinishing) {
                snake.speed -= 0.05;
                //snake.radius -= 10;
            }
        }, bonusType.time) ;
    }
    else if (bonusType.name == BonusNames.SLOW_ME || bonusType.name == BonusNames.SLOW_THEY) {
        if (snake.speed > 0.025) {
            snake.speed -= 0.025;
        }
        else
            snake.speed = 0.025;
        //snake.radius -= 10;
        console.log('changed SLOW');
        setTimeout(function () {
            if (snake.bonusFinishing) {
                snake.speed += 0.025;
                //snake.radius = StartRadius;
            }
        }, bonusType.time);
    }
    // FAT
    else if (bonusType.name == BonusNames.FAT_ME || bonusType.name == BonusNames.FAT_THEY) {
        snake.thickness *= 2;
        console.log('changed FAT');
        setTimeout(function () {
            if (snake.bonusFinishing) {
                snake.thickness /= 2;
            }
        }, bonusType.time);
    }
    // THIN
    else if (bonusType.name == BonusNames.THIN_ME || bonusType.name == BonusNames.THIN_THEY) {
        if (snake.thickness / 2 >= 2) {
            snake.thickness /= 2;
        }
        else
            snake.thickness = 2;
        console.log('changed THIN');
        setTimeout(function () {
            if (snake.bonusFinishing) {
                snake.thickness *= 2;
            }
        }, bonusType.time);
    }
    // FLYING
    else if (bonusType.name == BonusNames.FLYING) {
        snake.fly(bonusType.time/1000 + snake.flying);
    }
    // CLEAN_ME
    else if (bonusType.name == BonusNames.CLEAN_ME) {
        snake.cleanColisionTable();
        snake.tempCtx.clearRect(0, 0, gameHeight, gameWidth);
        snake.ctx.clearRect(0, 0, gameHeight, gameWidth);
        // usuniecie sciezki wezyka z glownej tablicy kolizji
        for (var i = 0; i < gameHeight; i++) {
            for (var j = 0; j < gameWidth; j++) {
                if (colisionTable[i][j] == snake.number)
                    colisionTable[i][j] = undefined;
            }
        }
    }
    // WALL
    else if (bonusType.name == BonusNames.WALL_ALL) {
        snake.goWall(bonusType.time/1000 + snake.wall);
    }
}

function ExecuteBonus(bonusType) {
    if (bonusType.name == BonusNames.CLEAN_ALL) {
        console.log("CLEAN_ALL");
        // wyczyszczenie glownej tablicy kolizji
        CleanColisionTable();
        ctxMain.clearRect(0, 0, gameWidth, gameHeight);
        ctx.clearRect(0, 0, gameWidth, gameHeight);

        //wyczyszczenie pojedynczych tablic dla wszystkich wezykow
        for (var j = 0; j < snakesNumber; j++) {
            snakes[j].cleanColisionTable();
            snakes[j].tempCtx.clearRect(0, 0, gameHeight, gameWidth);
            snakes[j].ctx.clearRect(0, 0, gameHeight, gameWidth);
        }
    }
}